'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 战斗排行版
 * @author sundbcn@gmail.com (sun debing)
 */

var _ = require('lodash');
var async = require('async');
var battleModel = require('../models/battleModel');
var roleModel = require('../models/roleModel');
var PlayerManager = require('../libs/playerManager');
var unionGroupModel = require('../models/unionGroupModel');
var roleService = require('./roleService');

/**
 * 生成玩家排行版
 * @param {[type]} battle_list [description]
 */
exports.getCupRank = function (cb) {
    _getBattleRankInfo(function (err, battle_list) {
        // 前50名玩家排行数据
        cb(null, battle_list);
    });
};

/**
 * 获取玩家排名
 * @param {[type]} current_role_info [description]
 */
exports.getCurrentRoleRank = function (role_id, cb) {
    _getRoleRank(role_id, function (err, current_role_info) {
        // 当前玩家排名
        cb(null, current_role_info);
    });
};

/**
 * 当前玩家排名
 * @param {[type]} reward_list [description]
 */
var _getRoleRank = function (role_id, cb) {
    var current_role_info = {};

    var battle_model;
    var battle_rank = 0;
    async.waterfall(
        [
            function (cb) {
                battleModel.getByRoleId(role_id, cb);
            },
            function (model, cb) {
                battleModel.getCupRankByRoleId(role_id, model.getCup(), cb);
            },
            function (rank, cb) {
                battle_rank = rank;
                roleService.getRoleInfo(role_id, cb);
            },
            function (role_info, cb) {
                current_role_info = role_info;
                current_role_info.rank = battle_rank;
                cb();
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
            }
            cb(null, current_role_info);
        }
    );
};

/**
 * 排行版相关信息
 * @param {[type]} reward_list [description]
 */
var _getBattleRankInfo = function (cb) {
    battleModel.getCupTopFifty(function (err, top_fifty_role) {
        _getBattleRoleName(top_fifty_role, function (err, battle_list) {
            var rank = 1;
            _.each(battle_list, function (role_item) {
                if (!!role_item) {
                    role_item.rank = rank++;
                }
            });
            cb(null, battle_list);
        });
    });
};

/**
 * 获取排行版玩家名字
 */
var _getBattleRoleName = function (top_fifty_role, cb) {
    var top_fifty_role_battle_list = [];
    var i = 0;
    async.whilst(
        function () {
            return i < top_fifty_role.length;
        },
        function (cb) {
            roleService.getRoleInfo(top_fifty_role[i].role_id, function (err, role_battle_item) {
                if (role_battle_item) {
                    top_fifty_role_battle_list.push(role_battle_item);
                }
                i++;
                cb();
            });
        },
        function (err) {
            if (!!err) {
                console.error(err);
            }
            cb(null, top_fifty_role_battle_list);
        }
    );
};
